package com.test.deencredit.bean_deencredit.process_deencredit;

import android.text.TextUtils;

import com.google.gson.JsonArray;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessInfo_Data_deencredit {

    private ProcessInfo_Data_deencredit(){}

    private String title;
    private String hint;
    private String paramName;
    private String action;
    private String isMust;
    private JsonArray values;
    private String paramNameState;
    private String paramNameCity;
    private String paramNameArea;
    private String paramNameBankName;
    private String paramNameBankCode;

    private ArrayList<String> relationships;
    private String[] bankNames;

    private ArrayList<JSONObject> valueJSONs;

    public String getTitle_deencredit() {
        if (TextUtils.isEmpty(title)) return "";
        return title;
    }

    public String getHint_deencredit() {
        if (TextUtils.isEmpty(hint)) return "";
        return hint;
    }

    public String getParamName_deencredit() {
        if (TextUtils.isEmpty(paramName)) return "";
        return paramName;
    }

    public String getAction_deencredit() {
        if (TextUtils.isEmpty(action)) return "";
        return action;
    }

    public boolean isMustInput_deencredit(){
        if (TextUtils.isEmpty(isMust)) return true;
        return !isMust.equals("NO");
    }

    public void setRelationships_deencredit(ArrayList<String> relationships) {
        this.relationships = relationships;
    }

    public void setBankNames_deencredit(String[] bankNames) {
        this.bankNames = bankNames;
    }

    public String[] getTitlesOfValue_deencredit(){

        if (relationships!=null) return relationships.toArray(new String[0]);

        if (bankNames!=null) return bankNames;

        int size = values.size();

        if (valueJSONs==null||valueJSONs.isEmpty()){
            valueJSONs = new ArrayList<>();
            for (int i=0;i<size;i++){
                try {
                    valueJSONs.add(new JSONObject(values.get(i).toString()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        size = valueJSONs.size();
        String[] result = new String[size];

        for (int i=0;i<size;i++){
            result[i] = valueJSONs.get(i).optString("title");
        }

        return result;
    }

    public String getParamNameState_deencredit() {
        if (TextUtils.isEmpty(paramNameState)) return "";
        return paramNameState;
    }

    public String getParamNameCity_deencredit() {
        if (TextUtils.isEmpty(paramNameCity)) return "";
        return paramNameCity;
    }

    public String getParamNameArea_deencredit() {
        if (TextUtils.isEmpty(paramNameArea)) return "";
        return paramNameArea;
    }

    public String getParamNameBankName_deencredit() {
        if (TextUtils.isEmpty(paramNameBankName)) return "";
        return paramNameBankName;
    }

    public String getParamNameBankCode_deencredit() {
        if (TextUtils.isEmpty(paramNameBankCode)) return "";
        return paramNameBankCode;
    }

    public String getValueOfTitle_deencredit(String title){

        if (valueJSONs==null||valueJSONs.isEmpty()){
            valueJSONs = new ArrayList<>();
            for (int i=0;i<values.size();i++){
                try {
                    valueJSONs.add(new JSONObject(values.get(i).toString()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        for (JSONObject valueJSON:valueJSONs){
            if (valueJSON.optString("title").equalsIgnoreCase(title)) return valueJSON.optString("value");
        }

        return "";
    }
}
